<?php namespace Core\Myapp\Router;

class Standard
{
    protected $_module;
    protected $_controller;
    protected $_action;
    protected $_params;

    public function match(\Core\Zend\Controller\Request\Request $request)
    {
        $path = trim($request->getPathInfo(), '/');
        if(!$path) {
            $this->_module = 'cms';
            $this->_controller = 'Modules\Core\Cms\Controller\PageController';
            $this->_action = 'ViewAction';
            return true;
        }
        $segments = explode('/', $path);
        $this->_moduleName = isset($segments[0]) ? $segments[0] : 'default_module';
        $this->_controllerName = isset($segments[1]) ? $segments[1] : 'index';
        $this->_actionName = isset($segments[2]) ? $segments[2] : 'index';

        // set parameters from pathinfo
        for ($i = 3, $l = sizeof($segments); $i < $l; $i += 2) {
            $request->setParam($segments[$i], isset($segments[$i+1]) ? urldecode($segments[$i+1]) : '');
        }
        $this->_params = $request->getParams();
    }
    public function getModule()
    {
        return $this->_module;
    }

    public function getController()
    {
        return $this->_controllerName;
    }

    public function getAction()
    {
        return $this->_action;
    }

    public function getParams()
    {
        return $this->_params;
    }
    protected function getControllerInstance()
    {
        return new $this->_controller;
    }
    public function run()
    {
        $method = $this->_action;
        $this->getControllerInstance()->$method();
    }
}